/*
  LinPVR - Linux Personal Video Recorder
  Copyright (C) 2006 Kamil Pawlowski <kamilpe@gmail.com>

  This program is free software; you can redistribute it and/or
  modify it under the terms of the GNU General Public License
  as published by the Free Software Foundation; either version 2
  of the License, or (at your option) any later version.
  
  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
  
  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
*/
#include <glib.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <linux/videodev2.h>
#include "capture.h"
#include "debug.h"

/* --- private functions --- */

gpointer *video_mainloop(capture_t *capture);

/* --- bodies of functions ---*/

int capture_create(capture_t *capture, int width, int height)
{
  DEBUG_ENTER("capture_create");
  capture->width                  = width;
  capture->height                 = height;
  capture->state                  = CAPTURE_NO;
  capture->timestamp              = 0;
  capture->transfered             = 0;
  capture->video_proc.rgb_buffer  = g_malloc(width * height * 3);
  capture->video_proc.frame_ready = 0;
  capture->video_proc.device      = NULL;
  capture->video_proc.encoder     = NULL;
  capture->video_proc.iframe      = NULL;
  capture->video_proc.oframe      = NULL;
  capture->video_proc.init_device = NULL;
  capture->video_proc.stop_device = NULL;
  capture->video_proc.thread      = NULL;

  if (capture->video_proc.rgb_buffer == NULL)
    return ERR_MALLOC;

  return 0;
}

void capture_destroy(capture_t *capture)
{
  DEBUG_ENTER("capture_destroy");
  g_free(capture->video_proc.rgb_buffer);
}

int capture_start(capture_t *capture) 
{
  int ret;
  DEBUG_ENTER("capture_start");

  capture->state = CAPTURE_STARTING;
  
  /* start video procesor thread */
  if (capture->video_proc.init_device != NULL)
    if ((ret = capture->video_proc.init_device(capture)) < 0)
      return ret;
    

  capture->video_proc.thread = g_thread_create((GThreadFunc)video_mainloop, capture, TRUE, NULL);
  capture->state = CAPTURE_WORK;
}

int capture_stop(capture_t *capture) 
{
  int ret;
  DEBUG_ENTER("capture_stop");

  capture->state = CAPTURE_STOPPING;

  g_thread_join(capture->video_proc.thread);

  if (capture->video_proc.stop_device != NULL)
    if ((ret = capture->video_proc.stop_device(capture)) < 0)
      return ret;

  capture->video_proc.frame_ready = 0;
  capture->state = CAPTURE_NO;
}

gpointer *video_mainloop(capture_t *capture) 
{
  DEBUG_ENTER("video_mainloop");

  while(capture->state != CAPTURE_STOPPING) {
    if (capture->video_proc.iframe != NULL)
      capture->video_proc.iframe(capture);
    if (capture->video_proc.oframe != NULL)
      capture->video_proc.oframe(capture);
  }

  g_thread_exit(NULL);
}
